//
//  THCPCPublishNewVC.m
//  YJB
//
//  Created by 中电大维 on 2016/12/29.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THCPCPublishNewVC.h"
#import "THCPCObjectView.h"
#import "THCPCPublishFixVC.h"
#import "PopoverView.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "THTaskProtocolVC.h"
#import "THCpcMyPublishModel.h"
#import "THCPCDescriptionView.h"
#import "THCPCTitleView.h"
#import "THDescriptionModel.h"

@interface THCPCPublishNewVC ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,UITextFieldDelegate,DescriptionDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIScrollViewDelegate>
@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *descrptions;
@end

@implementation THCPCPublishNewVC
{
    CGFloat     _titleWidth;//"任务标题" 的宽度
    THCPCTitleView      *_taskTitleView;//任务标题
    THCPCTitleView      *_adverUrlView;//广告网址
    THCPCTitleView      *_programView;//点击方案
    THCPCTitleView      *_descreptionView;//点击方案
//    UIView      *_descreptionView;//任务描述
    
    UITextView  *_titleTextView;//任务标题内容框
    UILabel     *_titleCountLabel;//字数统计
    UITextField *_advertUrlTextField;//输入广告网址框
    UILabel     *_programLabel;//点击方案文本内容
    UIButton    *_programButton;
    
    UITextField *_advertCountField;
    UITextField *_unitPriceField;
    UITextField *_totalPriceField;
    UIButton    *iconButton;
    UIView      *iconGroundView;
    
    THCPCDescriptionView      *_keyword1View;//在打开的网址搜索以下关键字
    UIButton *_currentIcon;
    UIView                    *_operationView;
    NSArray     *_programs;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self setUpNav];
    [self createSubView];
}

- (void)addDescriptionImage:(UIButton *)button
{
//    _curEditView = button.superview;
//    
//    MyLog(@"%ld",(long)_curEditView.tag);
    
    [self addicon:button];
}
- (void)publishTask
{
    THCpcMyPublishModel *model = [[THCpcMyPublishModel alloc] init];
    model.title = _titleTextView.text;
    model.url = _advertUrlTextField.text;
    model.method = _programLabel.text;
    model.adsCount = _advertCountField.text;
    model.price = _unitPriceField.text;
    model.totalgold = _totalPriceField.text;
    if (![iconButton.imageView.image isEqual:Image(@"add_image_default")]) {
        model.iconimage = iconButton.imageView.image;
    }
    for (THCPCDescriptionView *view in self.descrptions) {
        THDescriptionModel *desmodel = [[THDescriptionModel alloc] init];
        desmodel.content = view.desTextView.text;
        if (![view.iconButton.imageView.image isEqual:Image(@"task_placeholderImg_icon")]) {
            desmodel.image = view.iconButton.imageView.image;
        }
        [model.descriptions addObject:desmodel];
    }
//    model.descString = @"在打开的网页搜索以下关键字";
//    model.advertImage = iconButton.imageView.image;
    
    THCPCPublishFixVC *vc = [[THCPCPublishFixVC alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

//发布
- (void)publishClick{
    if (![NSString whiteString:_titleTextView.text]) {
        [MBProgressHUD showError:@"请输入任务标题"];
        return;
    }
    NSArray *messages = @[@"请输入广告网址",@"请输入广告数量",@"请输入单价"];
    NSArray *textFields = @[_advertUrlTextField,_advertCountField,_unitPriceField];
    for (int i = 0; i < messages.count ; i ++)
    {
        UITextField *textField = textFields[i];
        if ([NSString whiteString:textField.text] == 0){
            [MBProgressHUD showError:messages[i]];
            return;
        }
        if (i == 0 && ![textField.text isUrlString]){
            [MBProgressHUD showError:@"任务地址格式不对"];
            return;
        }
    }
    [self publishTask];
}
//取消
- (void)cancelClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)programClick:(UIButton *)button
{
    button.selected = !button.selected;
    if (button.selected) {
        self.tableView.hidden = NO;
    }else{
        self.tableView.hidden = YES;
    }
}

- (void)initData{
    _titleWidth = [NSString widthForString:@"任务标题:" fontSize:15 andHeight:15];
    _programs = [[NSArray alloc] initWithObjects:@"1天内相同IP不重复点击",@"1天内相同网段不重复点击",@"3天内相同网段不重复点击",@"7天内相同网段不重复点击",@"15天内相同网段不重复点击", nil];
    _currentIcon = [[UIButton alloc] init];
    //_curEditView = [[THCPCDescriptionView alloc] init];
    
}

- (void)addicon:(UIButton *)button{
    
    _currentIcon = button;
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
    [self presentViewController:imagePickerController animated:YES completion:^{
        
    }];
}
#pragma mark - textView
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (_programButton.selected) {
        [self programClick:_programButton];
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
    if(textView.tag != 11){
        if (textView.text.length>30) {
            textView.text = [textView.text substringWithRange:NSMakeRange(0, 30)];
        }else{
            _titleCountLabel.text = [NSString stringWithFormat:@"%lu/30",(unsigned long)textView.text.length];
        }
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    
}

#pragma mark - textField
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    int total = [_advertCountField.text intValue]*[_unitPriceField.text intValue];
    _totalPriceField.text = [NSString stringWithFormat:@"%d",total];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (_programButton.selected) {
        [self programClick:_programButton];
    }
    if (textField.tag == 3) {
        return NO;
    }
    return YES;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];
    image = [self compressImage:image toTargetWidth:400];
    [_currentIcon setImage:image forState:UIControlStateNormal];
}

- (UIImage *)compressImage:(UIImage *)sourceImage toTargetWidth:(CGFloat)targetWidth {
    CGSize imageSize = sourceImage.size;
    
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    CGFloat targetHeight = (targetWidth / width) * height;
    
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}



- (void)test
{
    //_titleTextView.text = @"说到iOS自动布局，有很多的解决办法。有的人使用自动布局";
    _titleCountLabel.text = [NSString stringWithFormat:@"%lu/30",30-_titleTextView.text.length];
    
    _advertUrlTextField.placeholder = @"请输入网址以http://开头";
    //_advertUrlTextField.text = @"http://www.5iad.cn/zhuan.asp";
    _programLabel.text = @"一天内相同IP不重复点击";
    
//    self.scrollView.contentSize = CGSizeMake(self.view.width, 1000);
    
//    self.scrollView.contentSize = CGSizeMake(IPHONE_W, IPHONE_H*3);
    
//    _keyword1View.backgroundColor = [UIColor orangeColor];
}
- (void)addDescription
{
    if (self.descrptions.count >= 7) {
        [MBProgressHUD showError:@"最多添加7个描述项"];
        return;
    }
    
    THCPCDescriptionView *lastView = [self.descrptions lastObject];
    THCPCDescriptionView *currentView = [[THCPCDescriptionView alloc] init];
    currentView.delegate = self;
    [self.scrollView addSubview:currentView];
    if (self.descrptions.count == 1) {
        
        [lastView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(_descreptionView.mas_bottom);
            make.height.mas_equalTo(140);
        }];
    }
    
    [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(lastView.mas_bottom);
        make.height.mas_equalTo(140);
    }];
    
    [_operationView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(currentView.mas_bottom);
        make.height.mas_equalTo(80);
        make.bottom.equalTo(self.scrollView.mas_bottom).offset(-12);
    }];
    currentView.tag = self.descrptions.count+1;
    [self.descrptions addObject:currentView];
}

- (void)delDescription
{
    
    MyLog(@"删除新目标网址");
    if (self.descrptions.count == 1) {
        [MBProgressHUD showError:@"至少包含一个描述组"];
        return;
    }
    
    THCPCDescriptionView *lastView = [self.descrptions lastObject];
    [lastView removeFromSuperview];
    [self.descrptions removeObject:lastView];
    
    THCPCDescriptionView *currentView = [self.descrptions lastObject];
    [_operationView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(currentView.mas_bottom);
        make.height.mas_equalTo(80);
        make.bottom.equalTo(self.scrollView.mas_bottom).offset(-12);
    }];
}

- (void)objectUrlClick:(UIButton *)button
{
    if (button.tag == 1) {
        [self addDescription];
    }else{
        [self delDescription];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _programs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"puvlishNew"];
    if (!cell) {
        cell = [[UITableViewCell alloc] init];
        cell.textLabel.textColor = kUIColorFromRGB(0x333333);
        cell.backgroundColor = kUIColorFromRGB(0xeeeeeee);
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    cell.textLabel.text = _programs[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _programLabel.text = _programs[indexPath.row];
    [self programClick:_programButton];
}
#pragma mark - createSubView ,setFrame
- (void)createSubView
{
    NSArray *titles = [[NSArray alloc] initWithObjects:@"任务标题",@"广告网址",@"点击方案",@"任务描述", nil];
    

    _taskTitleView = [[THCPCTitleView alloc] init];
    _taskTitleView.title = [NSString stringWithFormat:@"%@:",titles[0]];
    
    _adverUrlView = [[THCPCTitleView alloc] init];
    _adverUrlView.title = [NSString stringWithFormat:@"%@:",titles[1]];
    _programView = [[THCPCTitleView alloc] init];
    _programView.title = [NSString stringWithFormat:@"%@:",titles[2]];
    _descreptionView = [[THCPCTitleView alloc] init];;
    _descreptionView.title = [NSString stringWithFormat:@"%@:",titles[3]];
    
    _titleTextView = [[UITextView alloc] init];
    _titleCountLabel = [[UILabel alloc] init];
    _advertUrlTextField = [[UITextField alloc] init];
    _programLabel = [[UILabel alloc] init];
    _programButton = [[UIButton alloc] init];
    
    _titleTextView.delegate = self;
    
    [_programButton setImage:Image(@"arrow_down") forState:UIControlStateNormal];
    [_programButton addTarget:self action:@selector(programClick:) forControlEvents:UIControlEventTouchUpInside];
    _keyword1View = [[THCPCDescriptionView alloc] init];
    _keyword1View.delegate = self;
    
    [self.scrollView addSubview:_taskTitleView];
    [self.scrollView addSubview:_adverUrlView];
    [self.scrollView addSubview:_programView];
    [self.scrollView addSubview:_descreptionView];
    
    [_taskTitleView addSubview:_titleTextView];
    [_taskTitleView addSubview:_titleCountLabel];
    [_adverUrlView addSubview:_advertUrlTextField];
    [_programView addSubview:_programLabel];
    [_programView addSubview:_programButton];
    
    [_scrollView addSubview:_keyword1View];
    //    [_descreptionView addSubview:_keyword2View];
    //    _titleCountLabel.textColor = [UIColor colorWithHexString:@""]
    _titleCountLabel.font = FONT(14);
    _titleTextView.font = FONT(16);
    _advertUrlTextField.font = FONT(16);
    _programLabel.font = FONT(16);
    
    _titleTextView.textColor = kUIColorFromRGB(0x333333);
    _titleCountLabel.textColor = kUIColorFromRGB(0x333333);
    _advertUrlTextField.textColor = kUIColorFromRGB(0x333333);
    _programLabel.textColor = kUIColorFromRGB(0x333333);
    _titleCountLabel.textAlignment = NSTextAlignmentRight;
    
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view];
    
    UILabel *advertCountLabel = [[UILabel alloc] init];
    advertCountLabel.text = @"广告数量:";
    advertCountLabel.font = FONT(15);
    advertCountLabel.textColor = [UIColor colorWithHexString:@"ff783c"];
    [view addSubview:advertCountLabel];
    
    _advertCountField = [[UITextField alloc] init];
    _advertCountField.backgroundColor = [UIColor colorWithHexString:@"ff783c" alpha:0.05];
    _advertCountField.placeholder = @"不低于10个";
    _advertCountField.textColor = [UIColor colorWithHexString:@"666666"];
    _advertCountField.font = FONT(15);
    _advertCountField.delegate = self;
    _advertCountField.tag = 1;
    [view addSubview:_advertCountField];
    
    UILabel *unitPriceLabel = [[UILabel alloc] init];
    unitPriceLabel.text = @"单价:";
    unitPriceLabel.textColor = [UIColor colorWithHexString:@"ff783c"];
    unitPriceLabel.font = FONT(15);
    [view addSubview:unitPriceLabel];
    
    _unitPriceField = [[UITextField alloc] init];
    _unitPriceField.backgroundColor = [UIColor colorWithHexString:@"ff783c" alpha:0.05];
    _unitPriceField.placeholder = @"不低于10金币";
    _unitPriceField.textColor = [UIColor colorWithHexString:@"666666"];
    _unitPriceField.font = FONT(15);
    _unitPriceField.delegate = self;
    _unitPriceField.tag = 2;
    [view addSubview:_unitPriceField];
    
    THCPCTitleView *totalPriceView = [[THCPCTitleView alloc] init];
    totalPriceView.title = @"广告总价:";
    [self.scrollView addSubview:totalPriceView];
    
    _totalPriceField = [[UITextField alloc] init];
    _totalPriceField.delegate = self;
    _totalPriceField.tag = 3;
    _totalPriceField.textColor = [UIColor colorWithHexString:@"666666"];
    _totalPriceField.font = FONT(15);
    
    [totalPriceView addSubview:_totalPriceField];
    
    _operationView = [[UIView alloc] init];
    _operationView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_operationView];
    
    UIButton *addObjectBtn = [[UIButton alloc] init];
    UIButton *delObjectBtn = [[UIButton alloc] init];
    
    addObjectBtn.titleLabel.font = FONT(15);
    delObjectBtn.titleLabel.font = FONT(15);
    [addObjectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [delObjectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addObjectBtn setBackgroundColor:[UIColor colorWithHexString:@"ff783c"]];
    [delObjectBtn setBackgroundColor:[UIColor colorWithHexString:@"dc462d"]];
    [delObjectBtn setBackgroundColor:[UIColor redColor]];
    
    [addObjectBtn.layer setMasksToBounds:YES];
    [addObjectBtn.layer setCornerRadius:3.0];
    [delObjectBtn.layer setMasksToBounds:YES];
    [delObjectBtn.layer setCornerRadius:3.0];
    
    [addObjectBtn setTitle:@"添加新描述组" forState:UIControlStateNormal];
    [delObjectBtn setTitle:@"删除最后描述组" forState:UIControlStateNormal];
    
    addObjectBtn.tag = 1;
    delObjectBtn.tag = 2;
    
    [addObjectBtn addTarget:self action:@selector(objectUrlClick:) forControlEvents:UIControlEventTouchUpInside];
    [delObjectBtn addTarget:self action:@selector(objectUrlClick:) forControlEvents:UIControlEventTouchUpInside];
    [_operationView addSubview:addObjectBtn];
    [_operationView addSubview:delObjectBtn];
    
    
    [addObjectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_operationView.mas_centerY);
        make.left.mas_equalTo(22.5);
        make.height.mas_equalTo(40);
    }];
    [delObjectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_operationView.mas_centerY);
        make.left.equalTo(addObjectBtn.mas_right).offset(30);
        make.right.mas_equalTo(-22.5);
        make.height.mas_equalTo(40);
        make.width.equalTo(addObjectBtn.mas_width);
    }];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_programView.mas_bottom).offset(5);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(52);
    }];
    [advertCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(_titleWidth);
        make.height.equalTo(view.mas_height);
    }];
    [_advertCountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(advertCountLabel.mas_centerY);
        make.left.equalTo(advertCountLabel.mas_right).offset(5);
        make.right.equalTo(view.mas_centerX).offset(-20);
        make.height.equalTo(view.mas_height).multipliedBy(0.65);
    }];
    [unitPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(advertCountLabel.mas_centerY);
        make.left.equalTo(view.mas_centerX);
        make.width.mas_equalTo(_titleWidth*0.6);
        make.height.equalTo(view.mas_height);
    }];
    [_unitPriceField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(advertCountLabel.mas_centerY);
        make.left.equalTo(unitPriceLabel.mas_right).offset(5);
        make.right.equalTo(view.mas_right).offset(-20);
        make.height.mas_equalTo(_advertCountField.mas_height);
    }];
    [totalPriceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).offset(5);
        make.left.mas_equalTo(0);
        make.height.equalTo(view.mas_height);
        make.width.equalTo(view.mas_width);
    }];
    
    UILabel *label = [totalPriceView viewWithTag:1];
    [_totalPriceField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(totalPriceView.mas_centerY);
        make.left.equalTo(label.mas_right).offset(35);
        make.right.equalTo(totalPriceView.mas_right).offset(-12);
    }];
    
    iconGroundView = [[UIView alloc] init];
    iconGroundView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:iconGroundView];
    
    [iconGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(115);
        make.top.equalTo(totalPriceView.mas_bottom).offset(5);
        make.left.right.equalTo(totalPriceView);
    }];
    
    iconButton = [[UIButton alloc] init];
    [iconButton setImage:Image(@"add_image_default") forState:UIControlStateNormal];
    [iconButton addTarget:self action:@selector(addicon:) forControlEvents:UIControlEventTouchUpInside];
    [iconGroundView addSubview:iconButton];
    
    [iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconGroundView.mas_top).offset(15);
        make.left.equalTo(iconGroundView.mas_left).offset(12);
        make.width.height.mas_equalTo(60);
    }];
    
    
    UILabel *imageAlertLabel = [[UILabel alloc] init];
    imageAlertLabel.text = @"请上传一张广告图标，若没有，可以不上传";
    imageAlertLabel.font = FONT(15);
    [iconGroundView addSubview:imageAlertLabel];
    
    [imageAlertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconButton.mas_bottom).offset(10);
        make.left.equalTo(iconButton.mas_left);
        make.right.equalTo(iconGroundView.mas_right).offset(-12);
        make.height.mas_equalTo(15);
    }];
    
    [self.scrollView addSubview:self.tableView];
    [self mas_makeConstraints];
    [self test];
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
//        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.userInteractionEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.backgroundColor = kUIColorFromRGB(0xeeeeee);
        // 是否反弹
        _scrollView.bounces = NO;
        [_scrollView flashScrollIndicators];
        [self.view addSubview:_scrollView];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _scrollView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.hidden = YES;
        [self.scrollView addSubview:_tableView];
    }
    return _tableView;
}
#pragma mark -  mas_makeConstraints
- (void)mas_makeConstraints
{
    [_taskTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(72);
        make.width.mas_equalTo(IPHONE_W);
    }];
    [_adverUrlView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_taskTitleView.mas_bottom).offset(5);
        make.height.mas_equalTo(47);
    }];
    [_programView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_adverUrlView.mas_bottom).offset(5);
        make.height.mas_equalTo(49);
        
    }];
    UILabel *label = [_taskTitleView viewWithTag:1];
    [_titleTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(7);
        make.top.mas_equalTo(14);
        make.bottom.mas_equalTo(-14);
        make.right.equalTo(_titleCountLabel.mas_left).offset(-10);
    }];
    
    [_titleCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleTextView.mas_centerY);
        make.right.equalTo(_taskTitleView.mas_right).offset(-11);
        make.width.mas_equalTo(40);
    }];
    
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kUIColorFromRGB(0xff783c);
    [_adverUrlView addSubview:line];
    [_advertUrlTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_adverUrlView.mas_centerY).offset(-1.5);
        make.left.equalTo(_titleTextView.mas_left);
        make.right.equalTo(_adverUrlView.mas_right).offset(-11);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_advertUrlTextField.mas_left);
        make.top.equalTo(_advertUrlTextField.mas_bottom).offset(1.5);
        make.right.equalTo(_adverUrlView.mas_right).offset(-12);
        make.height.mas_equalTo(1);
    }];
    
    [_programLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_programView.mas_centerY);
        make.left.equalTo(_titleTextView.mas_left);
        make.right.equalTo(_programButton.mas_left).offset(-20);
    }];
    
    [_programButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_programView.mas_centerY);
        make.right.equalTo(_programView.mas_right).offset(-12);
        make.width.mas_equalTo(34);
        make.height.mas_equalTo(34);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_programView.mas_bottom);
        make.height.mas_equalTo(250);
    }];
    
    [_descreptionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(iconGroundView.mas_bottom).offset(5);
        make.height.mas_equalTo(49);
        
    }];
    [_keyword1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_descreptionView.mas_bottom);
        make.height.mas_equalTo(140);
    }];
    _keyword1View.tag = 1;
    [self.descrptions addObject:_keyword1View];
    [_operationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_keyword1View.mas_bottom);
        make.height.mas_equalTo(80);
        make.bottom.equalTo(self.scrollView.mas_bottom).offset(-12);
    }];
    
    
}
- (NSMutableArray *)descrptions
{
    if (!_descrptions) {
        _descrptions = [NSMutableArray array];
    }
    return _descrptions;
}

- (void)setUpNav{
    
    self.title = @"发布任务";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(publishClick)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelClick)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    if (_programButton.selected) {
        [self programClick:_programButton];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
